package edu.hcmus.sow.web.document;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import edu.hcmus.sow.domain.FileRevision;
import edu.hcmus.sow.domain.Folder;
import edu.hcmus.sow.service.DocumentService;

@Controller
public class DocumentController {

   // ~ Instance fields
   // ================================================================================================
   @Autowired
   private DocumentService documentService;

   // ~ Repository's methods
   // ================================================================================================
   @RequestMapping("/repository/{id}")
   public ModelAndView viewRepository(@PathVariable("id") Integer id) {
      ModelAndView mav = new ModelAndView();

      // get folders list which in parent folder
      List<Folder> folders = documentService.getFoldersByParentID(id);
      mav.addObject("folders", folders);

      // get files list which in the folder
      List<FileRevision> files = documentService.getLatestFilesVersionByFolderID(id);
      mav.addObject("files", files);

      // set parent folder
      mav.addObject("currentFolderID", id);
      mav.addObject("folderForm", new FolderForm(id));

      mav.setViewName("repository");

      return mav;
   }

}
